package com.bestcem.xm.user.grpc.service;

import com.alibaba.excel.util.StringUtils;
import com.bestcem.bp.xm.user.grpc.v1.services.UnBindWechatWorkRequest;
import com.bestcem.bp.xm.user.grpc.v1.services.UnBindWechatWorkResponse;
import com.bestcem.bp.xm.user.grpc.v1.services.UserWechatWorkServiceGrpc;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.service.UserService;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;

/**
 * 企业微信相关服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/29 15:20
 */
@Slf4j
@GrpcService
public class UserWechatWorkGrpcService extends UserWechatWorkServiceGrpc.UserWechatWorkServiceImplBase {

    @Resource
    private UserService userService;

    /**
     * 用户解绑企业微信
     *
     * @param request          请求信息
     * @param responseObserver 响应内容
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/29 15:22
     */
    @Override
    public void unBindWechatWork(UnBindWechatWorkRequest request, StreamObserver<UnBindWechatWorkResponse> responseObserver) {
        // 获取返回对象的构造器
        UnBindWechatWorkResponse.Builder builder = UnBindWechatWorkResponse.newBuilder();

        // 参数校验
        String orgId = request.getOrgId();
        String openUserid = request.getOpenUserid();
        if (StringUtils.isBlank(orgId) && StringUtils.isBlank(openUserid)) {
            builder.setStatus(RpcStatusResult.error("orgId和openUserid不能都为空"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }

        // 解绑企业微信
        ServiceResult<Void> result = userService.unbindWechatWork(orgId, openUserid);
        if (!result.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
        } else {
            builder.setStatus(RpcStatusResult.success());
        }

        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

}
